/*
 * create:2016年4月28日 上午9:46:48
 * author:wangds  
 */
package com.cxkj.shop.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.cxkj.shop.model.order.Order;
import com.cxkj.shop.model.order.OrderItem;
import com.cxkj.shop.proc.order.OrderItmeMap2EntityProc;
import com.cxkj.shop.proc.order.OrderMap2EntityProc;

import net.wangds.common.GeneralParentObject;

/**
 * .
 * @author wangds
 *
 */
public class OrderUtils extends AbstractBusinessUtils{


  public static Order queryOrderByOrderId(String orderId) {
    Order res = null;
    //TASK: TSK2016041304 查询订单
    //  FINISH:2016-04-28 by wangds@cixunkeji.com
    Collection<Map<String,Object>> list = execSelectList("selOrderByOrderId", orderId);
    Collection<Order> orders = transRecordMaps2Order(list);
    if(orders==null){
      return res;
    }
    Iterator<Order> it = orders.iterator();
    if(it.hasNext()){
      res = it.next();
    }
    return res;
  }
  
  public static Collection<Order> transRecordMaps2Order(Collection<Map<String, Object>> rcds){
    Collection<Order> res = new ArrayList<Order>();
    Order previous = null;
    OrderMap2EntityProc proc = new OrderMap2EntityProc();
    OrderItmeMap2EntityProc item_proc = new OrderItmeMap2EntityProc();
    for(Map<String,Object> cur:rcds){
      if(previous==null||(!previous.getOrderId().equals(cur.get("orderId")))){
        proc.setIntput(cur);
        try {
          proc.process();
        } catch (Exception e) {
          GeneralParentObject.staticLogger(OrderUtils.class).error(e.getLocalizedMessage(),e);
        }
        previous = proc.getResult();
        previous.setItems(new ArrayList<OrderItem>());
        res.add(previous);
      }
      
      item_proc.setIntput(cur);
      try {
        item_proc.process();
        previous.getItems().add(item_proc.getResult());
      } catch (Exception e) {
        GeneralParentObject.staticLogger(OrderUtils.class).error(e.getLocalizedMessage(),e);
      }
      
    }
    return res;
  }
  
  public static Collection<Order> transRecordMaps2OrderByTimerType(Collection<Map<String, Object>> rcds){
	    Collection<Order> res = new ArrayList<Order>();
	    Order previous = null;
	    OrderMap2EntityProc proc = new OrderMap2EntityProc();
	    OrderItmeMap2EntityProc item_proc = new OrderItmeMap2EntityProc();
	    for(Map<String,Object> cur:rcds){
	      if(previous==null||(!previous.getOrderId().equals(cur.get("orderId")))){
	        proc.setIntput(cur);
	        try {
	          proc.process();
	        } catch (Exception e) {
	          GeneralParentObject.staticLogger(OrderUtils.class).error(e.getLocalizedMessage(),e);
	        }
	        previous = proc.getResult();
	        previous.setItems(new ArrayList<OrderItem>());
	        res.add(previous);
	      }
	      
	      item_proc.setIntput(cur);
	      try {
	        item_proc.process();
	        previous.getItems().add(item_proc.getResult());
	      } catch (Exception e) {
	        GeneralParentObject.staticLogger(OrderUtils.class).error(e.getLocalizedMessage(),e);
	      }
	      
	    }
	    return res;
	  }
  
  public static boolean updateOrderOnPayZero(Order order){
    boolean res = true;
    
    String sql = "updTradePayInfo";
    Map<String,Object> map = new HashMap<String,Object>();
    long tm = System.currentTimeMillis();
    map.put("out_trade_no", order.getOrderId());
    map.put("wxPayId", "balance@"+tm);
    map.put("payTm", tm);
    execUpdateSql(sql, map);
    return res;
  }

}
